<?php
/**
 * 菜单设置
 */

class Setting_AdminMenuController extends Cms_AdminController {
	/**
	 * 初始化
	 *
	 */
	public function init() {
		parent::init ();
	}
	
	/**
	 * 菜单管理
	 */
	public function menumanageAction() {
		
		$this->view->setScriptFile ( 'admin/admin_common.js' );
		
		$tree = new Cms_Tree ();
		$tree->icon = array ('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ' );
		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		
		$DB_Menu = new DB_Menu ();
		$result = $DB_Menu->loadList ( '*', "", "listorder ASC,id DESC" );
		
		$array = array ();
		foreach ( $result as $r ) {
			$r ['str_manage'] = '<a href="/setting/adminmenu/add/parentid/' . $r ['id'] . '/">添加子菜单</a> | <a href="/setting/adminmenu/edit/id/' . $r ['id'] . '/">修改</a> | <a href="javascript:confirmurl(\'/setting/adminmenu/delete/id/' . $r ['id'] . '/\',\'' . '确认要删除 『' . $r [name] . ' 』 吗？' . '\')">删除</a> ';
			$array [] = $r;
		}
		
		$str = "<tr>
		<td align='center'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input-text-c'></td>
		<td align='center'>\$id</td>
		<td >\$spacer\$name</td>
		<td align='center'>\$str_manage</td>
		</tr>";
		$tree->init ( $array );
		$categorys = $tree->get_tree ( 0, $str );
		
		$this->view->assign ( 'categorys', $categorys );
		
		$this->render ( 'Setting/adminmenu/menumanage', null, true );
	}
	
	/**
	 * 添加菜单
	 */
	public function addAction() {
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		
		if (isset ( $dosubmit )) {
			
			$info = $this->_request->getPost ( 'info' );
			
			$Model_Menu = new Model_Menu ();
			if ($Model_Menu->add_menu ( $info )) {
				$this->view->perRedirect ( '数据菜单成功！', '/setting/adminmenu/menumanage/' );
			} else {
				$this->view->perRedirect ( '数据菜单失败！', '/setting/adminmenu/menumanage/' );
			}
		} else {
			$this->view->setScriptFile ( 'formvalidator.js' );
			$this->view->setScriptFile ( 'formvalidatorregex.js' );
			
			$tree = new Cms_Tree ();
			$DB_Menu = new DB_Menu ();
			$result = $DB_Menu->loadList ( '*', "", "listorder ASC,id DESC" );
			
			$parentid = $this->_request->get ( 'parentid' );
			
			$array = array ();
			foreach ( $result as $r ) {
				$r ['cname'] = $r ['name'];
				$r ['selected'] = $r ['id'] == $parentid ? 'selected' : '';
				$array [] = $r;
			}
			$str = "<option value='\$id' \$selected>\$spacer \$cname</option>";
			$tree->init ( $array );
			$select_categorys = $tree->get_tree ( 0, $str );
			
			$this->view->assign ( 'select_categorys', $select_categorys );
			
			$this->render ( 'Setting/adminmenu/menuadd', null, true );
		}
	}
	
	/**
	 * 编辑菜单
	 */
	public function editAction() {
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		if (isset ( $dosubmit )) {
			$id = $this->_request->getPost ( 'id' );
			
			$info = $this->_request->getPost ( 'info' );
			
			$Model_Menu = new Model_Menu ();
			
			if ($Model_Menu->edit_menu ( $id, $info )) {
				$this->view->perRedirect ( '编辑菜单成功！', '/setting/adminmenu/menumanage/' );
			} else {
				$this->view->perRedirect ( '编辑菜单失败！', "/setting/adminmenu/menumanage/edit/id/{$id}" );
			}
		
		} else {
			$id = $this->_request->get ( 'id' );
			
			$this->view->setScriptFile ( 'formvalidator.js' );
			$this->view->setScriptFile ( 'formvalidatorregex.js' );
			
			$tree = new Cms_Tree ();
			$DB_Menu = new DB_Menu ();
			$result = $DB_Menu->loadList ( '*', "", "listorder ASC,id DESC" );
			
			$parentid_Info = $DB_Menu->loadInfoByPrimary ( $id );
			$parentid = $parentid_Info ['parentid'];
			
			$array = array ();
			foreach ( $result as $r ) {
				$r ['cname'] = $r ['name'];
				$r ['selected'] = $r ['id'] == $parentid ? 'selected' : '';
				$array [] = $r;
			}
			$str = "<option value='\$id' \$selected>\$spacer \$cname</option>";
			$tree->init ( $array );
			$select_categorys = $tree->get_tree ( 0, $str );
			
			$Menu_info = $DB_Menu->loadInfoByPrimary ( $id );
			$this->view->assign ( 'Menu_info', $Menu_info );
			
			$this->view->assign ( 'select_categorys', $select_categorys );
			
			$this->view->assign ( 'id', $id );
			$this->render ( 'Setting/adminmenu/menuedit', null, true );
		}
	}
	
	/**
	 * 删除菜单
	 */
	public function deleteAction() {
		$id = $this->_request->get ( 'id' );
		
		$Model_Menu = new Model_Menu ();
		if ($Model_Menu->delete_menu ( $id )) {
			$this->view->perRedirect ( '操作成功！', '/setting/adminmenu/menumanage/' );
		} else {
			$this->view->perRedirect ( '操作失败！', '/setting/adminmenu/menumanage/' );
		}
	}
	
	/**
	 * 排序
	 */
	public function listorderAction() {
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		
		if (isset ( $dosubmit )) {
			$listorders = $this->_request->getPost ( 'listorders' );
			if (is_array ( $listorders )) {
				$DB_Menu = new DB_Menu ();
				foreach ( $listorders as $id => $listorder ) {
					$DB_Menu->updateByPrimary ( array ('listorder' => $listorder ), $id );
				}
				$this->view->perRedirect ( '操作成功！', '/setting/adminmenu/menumanage/' );
			}
			$this->view->perRedirect ( '操作失败！', '/setting/adminmenu/menumanage/' );
		}
		
		$this->view->perRedirect ( '操作失败！', '/setting/adminmenu/menumanage/' );
	}
}